home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / ace / c / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  16.4 KB  |  820 lines

  1. /* << ACE >>
  2.  
  3.    -- Amiga BASIC compiler --
  4.  
  5.    ** Symbol Table Management & Code Generation **
  6.    ** Copyright (C) 1998 David Benn
  7.    ** 
  8.    ** This program is free software; you can redistribute it and/or
  9.    ** modify it under the terms of the GNU General Public License
  10.    ** as published by the Free Software Foundation; either version 2
  11.    ** of the License, or (at your option) any later version.
  12.    **
  13.    ** This program is distributed in the hope that it will be useful,
  14.    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    ** GNU General Public License for more details.
  17.    **
  18.    ** You should have received a copy of the GNU General Public License
  19.    ** along with this program; if not, write to the Free Software
  20.    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    Author: David J Benn
  23.      Date: 26th October-25th November, 2nd-13th December 1991,
  24.        27th January 1992, 8th-9th, 25th February 1992,
  25.        21st April 1992,
  26.        8th,9th,13th,14th,28th June 1992,
  27.        2nd,3rd,4th,5th,17th,26th,28th,29th July 1992,
  28.        1st-3rd,8th,9th August 1992,
  29.        6th,22nd,29th December 1992,
  30.        13th January 1993,
  31.        2nd,28th February 1993,
  32.        12th,20th April 1993,
  33.        15th-18th,28th December 1993,
  34.        7th January 1994,
  35.        26th February 1994,
  36.        12th June 1994,
  37.        20th August 1994,
  38.        10th September 1994
  39. */
  40.  
  41. #include "symvar.c"
  42.  
  43. /* -- general functions -- */
  44. void kill_all_lists()
  45. {
  46.   lev=ONE;
  47.   while (lev >= ZERO) { kill_symtab(); --lev; }
  48.   puts("freeing code list...");
  49.   kill_code();  /* all other lists are freed by cleanup() */
  50. }
  51.  
  52. /* -- symbol table functions -- */
  53. void new_symtab()
  54. {
  55. /* create a new symbol table
  56.    at current level.
  57. */
  58.  
  59.  if ((tab_head[lev] = (SYM *)sym_alloc(sizeof(SYM),MEMF_ANY)) == NULL)
  60.  {
  61.   printf("Can't allocate memory for symbol table!\n");
  62.   early_exit=TRUE;
  63.   kill_all_lists();
  64.   cleanup();
  65.  }
  66.  tab_head[lev]->next = NULL;
  67. }
  68.  
  69. void kill_symtab()
  70. {
  71. /* free memory held by 
  72.    symbol table. 
  73. */
  74. BOOL past_head=FALSE;
  75. SYM  *curr,*temp;
  76.  
  77.  free_sym_alloc();
  78. }
  79.  
  80. void find_tab_tail()
  81. {
  82. /* find the end of the 
  83.    symbol table at current
  84.    level.
  85. */
  86.    
  87.  tab_tail = tab_head[lev];
  88.  while (tab_tail->next != NULL) tab_tail = tab_tail->next;
  89. }
  90.  
  91. BOOL exist(name,obj)
  92. char  *name;
  93. int   obj;
  94. {
  95. int oldlevel;
  96.  
  97.  oldlevel=lev;
  98.  
  99.  if ((obj == subprogram) || (obj == function) || 
  100.      (obj == definedfunc) || (obj == extfunc) || (obj == extvar) || 
  101.      (obj == constant) || (obj == structdef)) lev=ZERO; 
  102.  
  103.  curr_item = tab_head[lev]->next;
  104.  while (curr_item != NULL) 
  105.  {
  106.   if ((strcmp(curr_item->name,name) == 0) && (curr_item->object == obj))
  107.      { lev=oldlevel; return(TRUE); }
  108.   else
  109.      curr_item = curr_item->next;
  110.  }
  111.  
  112.  lev=oldlevel;
  113.  return(FALSE);
  114. }
  115.  
  116. void enter(name,typ,obj,dims)
  117. /* enter a symbol into symbol table */
  118. char  *name;
  119. int   typ,obj;
  120. int   dims;
  121. {
  122. int i;
  123.  
  124.  /* allocate memory for info' */
  125.  if ((new_item = (SYM *)sym_alloc(sizeof(SYM),MEMF_ANY)) == NULL)
  126. {
  127.   printf("Can't allocate memory for symbol table item!\n");
  128.   early_exit=TRUE;
  129.   kill_all_lists();
  130.   cleanup();
  131.  }
  132.  
  133.  if ((new_item->name = (char *)sym_alloc(strlen(name)+1,MEMF_ANY)) == NULL)
  134.  {  
  135.   printf("Can't allocate memory for symbol table item!\n");
  136.   early_exit=TRUE;
  137.   kill_all_lists();
  138.   cleanup();
  139.  }
  140.  
  141.  /* fill the node with info' */
  142.  strcpy(new_item->name,name);
  143.  new_item->type = typ;
  144.  new_item->object = obj;
  145.  new_item->dims = dims;
  146.  
  147.  /* string defaults */
  148.  if ((typ == stringtype) && (obj != array)) 
  149.  {
  150.   new_item->new_string_var=TRUE;
  151.   new_item->decl=undeclared;
  152.   new_item->size=MAXSTRLEN;
  153.  }
  154.  
  155.  if ((obj != label) && (obj != function) && (obj != constant) &&
  156.      (obj != extvar) && (obj != extfunc) && (obj != structdef))
  157.  {
  158.   /* how many bytes to reserve? */
  159.   if ((typ == shorttype) && (obj != array)) 
  160.      addr[lev] += 2;
  161.   else
  162.      addr[lev] += 4; /* long, single, string, array, sub, structure */
  163.  
  164.   new_item->address = addr[lev];
  165.  }
  166.  else 
  167.      new_item->address = 0; 
  168.  
  169.  new_item->level = lev; 
  170.  new_item->shared = FALSE;  /* shared may be explicitly SET later */
  171.  
  172.  /* array? -> store index maxima */
  173.  if (obj == array)
  174.  {
  175.   if ((new_item->index = (SHORT *)sym_alloc((dims+1)*2,MEMF_ANY)) == NULL)
  176.   {
  177.    printf("Array index storage allocation error.\n");
  178.    early_exit=TRUE;
  179.    kill_all_lists();  
  180.    cleanup();
  181.   }
  182.   else
  183.       for (i=0;i<=dims;i++) new_item->index[i] = dimsize[i];
  184.  }
  185.  
  186.  /* setup for structure definition */
  187.  if (obj == structdef)
  188.  {
  189.   if ((new_item->structmem = 
  190.                  (STRUCM *)sym_alloc(sizeof(STRUCM),MEMF_ANY)) == NULL)
  191.   {
  192.    printf("Can't allocate memory for an initial structdef node!\n");
  193.    early_exit=TRUE;
  194.    kill_all_lists();  
  195.    cleanup();
  196.   }
  197.   else
  198.   {
  199.    new_item->structmem->next = NULL;
  200.    new_item->size = 0;
  201.   }
  202.  }
  203.  
  204.  /* link item into symbol table */
  205.  find_tab_tail(tab_head[lev]);
  206.  tab_tail->next = new_item;
  207.  new_item->next = NULL;
  208.  curr_item = new_item;
  209. }
  210.  
  211. /*
  212. void show_table_item(item)
  213. SYM *item;
  214. {
  215.  printf("%10s\t",item->name);
  216.  showtyp(item->type);
  217.  showobj(item->object);
  218.  printf("%5d\t%5d\t%5d\n",item->dims,item->address,item->level);
  219. }
  220. */
  221.  
  222. /* --code generator functions-- */
  223.  
  224. void create_lists()
  225. {
  226. /* create code, DATA, BSS, XREF and BASIC DATA lists */
  227.  
  228.  data = (DATA *)alloc(sizeof(DATA),MEMF_ANY);
  229.  if (data == NULL) 
  230.  { 
  231.   cleanup(); 
  232.  }
  233.  else
  234.     { curr_data = data; data->next = NULL; }
  235.  
  236.  bss = (BSS *)alloc(sizeof(BSS),MEMF_ANY);
  237.  if (bss == NULL) 
  238.  { 
  239.   cleanup(); 
  240.  } 
  241.  else
  242.     { curr_bss = bss; bss->next = NULL; }
  243.  
  244.  xref = (XREF *)alloc(sizeof(XREF),MEMF_ANY);
  245.  if (xref == NULL)
  246.  {
  247.   cleanup();
  248.  }
  249.  else 
  250.      { curr_xref = xref; xref->next = NULL; }
  251.  
  252.  basdata = (BASDATA *)alloc(sizeof(BASDATA),MEMF_ANY);
  253.  if (basdata == NULL)
  254.  {
  255.   cleanup();
  256.  }
  257.  else
  258.     { curr_basdata = basdata; basdata->next = NULL; }
  259.  
  260.  code = (CODE *)alloc_code("  ","  ","  ");  /* first node is a dummy */
  261.  if (code == NULL) 
  262.  {
  263.     cleanup();
  264.  }
  265.  else
  266.      { curr_code = code; code->next = NULL; }
  267. }
  268.  
  269. BOOL is_a_label(opc)
  270. char *opc;
  271. {
  272. int cc=0;
  273.  
  274.  while (opc[cc] != '\0') cc++;
  275.  if (opc[cc-1] == ':') 
  276.     return(TRUE);
  277.  else
  278.     return(FALSE);
  279. }
  280.  
  281. void write_code(line)
  282. CODE *line;
  283. {
  284.  if (strcmp(line->opcode,"nop") != 0)
  285.  {
  286.   if (!is_a_label(line->opcode))
  287.   {
  288.    fprintf(dest,"\t%s",line->opcode);  
  289.    fprintf(dest,"\t%s",line->srcopr); 
  290.    if (line->destopr[0] != ' ')  
  291.       fprintf(dest,",%s\n",line->destopr);  /* comma & destopr */
  292.    else
  293.       fprintf(dest,"\n");  /* no destopr, so just LF */
  294.   }
  295.   else fprintf(dest,"%s\n",line->opcode);  /* label starts in 1st column */
  296.  }
  297. }
  298.  
  299. BOOL label_undef(node)
  300. CODE *node;
  301. {
  302. char buf[50];
  303.  
  304.  if ((strcmp(node->opcode,"jmp") == 0) ||
  305.     (strcmp(node->opcode,"jsr") == 0))
  306.  {
  307.   if (strcmp(node->destopr,"* ")==0)
  308.   {
  309.    /* undefined label at time of jmp/jsr */
  310.    strcpy(buf,node->srcopr);
  311.    strcat(buf,":\0");
  312.    /* has it been defined since jmp/jsr? */
  313.    if (exist(buf,label)) 
  314.       { 
  315.         strcpy(node->destopr,"  "); 
  316.         return(FALSE); 
  317.       }               /* not UNdefined */
  318.    else
  319.    {
  320.       early_exit = TRUE;
  321.       return(TRUE);   /* undefined */
  322.    }
  323.   }
  324.   else return(FALSE); /* not UNdefined */
  325.  }
  326.  else return(FALSE);  /* not a branch instruction */
  327. }
  328.  
  329. void undef_label_check()
  330. {
  331. BOOL past_head=FALSE;
  332.  
  333.  /* check for undefined labels */
  334.  curr_code = code;
  335.  while (curr_code->next != NULL) 
  336.  {
  337.   if (past_head)
  338.   {
  339.    if (label_undef(curr_code)) 
  340.       { _error(8); printf("'%s'\n",curr_code->srcopr); }
  341.   }
  342.   curr_code = curr_code->next;
  343.   if (!past_head) past_head=TRUE;
  344.  }
  345.  if (label_undef(curr_code)) 
  346.     { _error(8); printf("'%s'\n",curr_code->srcopr); }
  347. }
  348.   
  349. void kill_code()
  350. {
  351. BOOL past_head=FALSE;
  352. CODE *curr,*temp;
  353.  
  354.  curr=code;
  355.  
  356.  do
  357.  {
  358.   temp=curr;
  359.   curr=curr->next;
  360.   if (past_head) 
  361.   {
  362.    if (!early_exit) write_code(temp);
  363.  
  364.    /* free all the struct's memory */
  365.    free_code(temp);
  366.   }
  367.  
  368.   if (!past_head) past_head=TRUE;
  369.  }
  370.  while (curr != NULL);
  371.  
  372.  /* free the dummy head node's memory */
  373.  free_code(code);
  374. }
  375.  
  376. void gen(opcode,srcopr,destopr)
  377. char *opcode;
  378. char *srcopr;
  379. char *destopr;
  380. {
  381.  /* allocate memory for a new node & each field */
  382.  if ((new_code = (CODE *)alloc_code(opcode,srcopr,destopr)) == NULL)
  383.  {
  384.   printf("Can't allocate memory for code node!\n");
  385.   early_exit=TRUE;
  386.   kill_all_lists();
  387.   cleanup();
  388.  }
  389.  
  390.  /* fill code struct */
  391.  strcpy(new_code->opcode,opcode);
  392.  strcpy(new_code->srcopr,srcopr);
  393.  strcpy(new_code->destopr,destopr);
  394.  
  395.  new_code->next = NULL;
  396.  curr_code->next = new_code;
  397.  curr_code = curr_code->next;
  398. }
  399.  
  400. void change(cx,opcode,srcopr,destopr)
  401. CODE *cx;
  402. char *opcode;
  403. char *srcopr;
  404. char *destopr;
  405. {
  406.  /* free the old fields */
  407.  free_code_members(cx);
  408.  
  409.  /* allocate memory & insert new instruction & operands */
  410.  if ((BOOL)alloc_code_members(cx,opcode,srcopr,destopr))
  411.  {
  412.      strcpy(cx->opcode,opcode);
  413.      strcpy(cx->srcopr,srcopr);
  414.      strcpy(cx->destopr,destopr);
  415.  }
  416.  else
  417.  {
  418.   printf("Can't allocate memory for CODE node fields!\n");
  419.   early_exit=TRUE;
  420.   kill_all_lists();
  421.   cleanup();
  422.  }
  423. }
  424.  
  425. /* --DATA list functions-- */
  426.  
  427. BOOL exist_DATA(name)
  428. char *name;
  429. {
  430. DATA *curr;
  431.  curr = data->next;
  432.  while (curr != NULL) 
  433.  {
  434.   if (strcmp(curr->name,name) == 0)
  435.      return(TRUE);
  436.   else
  437.      curr = curr->next;
  438.  }
  439.  return(FALSE);
  440. }
  441.  
  442. void enter_DATA(name,literal)
  443. char *name;
  444. char *literal;
  445. {
  446.  if (exist_DATA(name)) return;  /* already exists */
  447.  
  448.  /* allocate memory for a new node & each field */
  449.  if ((new_data = (DATA *)alloc(sizeof(DATA),MEMF_ANY)) == NULL)
  450.  {
  451.   printf("Can't allocate memory for DATA node!\n");
  452.   early_exit=TRUE;
  453.   kill_all_lists();
  454.   cleanup();
  455.  }
  456.  
  457.  if ((new_data->name=(char *)alloc(strlen(name)+1,MEMF_ANY))==NULL)
  458.  {
  459.   printf("Can't allocate memory for DATA node name field!\n");
  460.   early_exit=TRUE;
  461.   kill_all_lists();
  462.   cleanup();
  463.  }
  464.  
  465.  if ((new_data->literal=(char *)alloc(strlen(literal)+1,MEMF_ANY))==NULL)
  466.  {
  467.   printf("Can't allocate memory for DATA node literal field!\n");
  468.   early_exit=TRUE;
  469.   kill_all_lists();
  470.   cleanup();
  471.  }
  472.  
  473.  /* fill DATA struct */
  474.  strcpy(new_data->name,name);
  475.  strcpy(new_data->literal,literal);
  476.  
  477.  new_data->next = NULL;
  478.  curr_data->next = new_data;
  479.  curr_data = curr_data->next;
  480. }
  481.  
  482. void write_data()
  483. {
  484. BOOL past_head=FALSE;
  485. DATA *curr,*temp;
  486.  
  487.  curr=data;
  488.  if (curr->next != NULL)
  489.  {
  490.   fprintf(dest,"\n\tSECTION data,DATA\n\n");
  491.  }
  492.  
  493.  do
  494.  {
  495.   temp=curr;
  496.   curr=curr->next;
  497.   if (past_head) 
  498.   {
  499.    fprintf(dest,"%s\t%s\n",temp->name,temp->literal);
  500.   }
  501.   if (!past_head) past_head=TRUE; 
  502.  }
  503.  while (curr != NULL);
  504. }
  505.       
  506. /* --BSS list functions-- */
  507.  
  508. BOOL exist_BSS(name)
  509. char *name;
  510. {
  511. BSS *curr;
  512.  curr = bss->next;
  513.  while (curr != NULL) 
  514.  {
  515.   if (strcmp(curr->name,name) == 0)
  516.      return(TRUE);
  517.   else
  518.      curr = curr->next;
  519.  }
  520.  return(FALSE);
  521. }
  522.  
  523. void enter_BSS(name,store)
  524. char *name;
  525. char *store;
  526. {
  527.  /* ignore if already exists, except if name is "  " 
  528.     which is used for structure declarations */ 
  529.  if ((exist_BSS(name)) && (strcmp(name,"  ") != 0)) return; 
  530.  
  531.  /* allocate memory for a new node & each field */
  532.  if ((new_bss = (BSS *)alloc(sizeof(BSS),MEMF_ANY)) == NULL)
  533.  {
  534.   printf("Can't allocate memory for BSS node!\n");
  535.   early_exit=TRUE;
  536.   kill_all_lists();
  537.   cleanup();
  538.  }
  539.  
  540.  if ((new_bss->name=(char *)alloc(strlen(name)+1,MEMF_ANY))==NULL)
  541.  {
  542.   printf("Can't allocate memory for BSS node name field!\n");
  543.   early_exit=TRUE;
  544.   kill_all_lists();
  545.   cleanup();
  546.  }
  547.  
  548.  if ((new_bss->store=(char *)alloc(strlen(store)+1,MEMF_ANY))==NULL)
  549.  {
  550.   printf("Can't allocate memory for BSS node literal field!\n");
  551.   early_exit=TRUE;
  552.   kill_all_lists();
  553.   cleanup();
  554.  }
  555.  
  556.  /* fill BSS struct */
  557.  strcpy(new_bss->name,name);
  558.  strcpy(new_bss->store,store);
  559.  
  560.  new_bss->next = NULL;
  561.  curr_bss->next = new_bss;
  562.  curr_bss = curr_bss->next;
  563. }
  564.  
  565. void write_bss()
  566. {
  567. BOOL past_head=FALSE;
  568. BSS  *curr,*temp;
  569.  
  570.  curr=bss;
  571.  if (curr->next != NULL)
  572.  { 
  573.   fprintf(dest,"\n\tSECTION mem,BSS\n\n");
  574.  }
  575.  
  576.  do
  577.  {
  578.   temp=curr;
  579.   curr=curr->next;
  580.   if (past_head) 
  581.   {
  582.    fprintf(dest,"%s\t%s\n",temp->name,temp->store);
  583.   }
  584.   if (!past_head) past_head=TRUE; 
  585.  }
  586.  while (curr != NULL);
  587. }
  588.  
  589. /* --XREF list functions-- */
  590.  
  591. BOOL exist_XREF(name)
  592. char *name;
  593. {
  594. XREF *curr;
  595.  
  596.  curr = xref->next;
  597.  while (curr != NULL) 
  598.  {
  599.   if (strcmp(curr->name,name) == 0)
  600.      return(TRUE);
  601.   else
  602.      curr = curr->next;
  603.  }
  604.  return(FALSE);
  605. }
  606.  
  607. void enter_XREF(name)
  608. char *name;
  609. {
  610.  if (exist_XREF(name)) return;   /* already exists */
  611.  
  612.  if (strcmp(name,"_DOSBase") == 0) dosused=TRUE;
  613.  if (strcmp(name,"_MathBase") == 0) mathffpused=TRUE;
  614.  if (strcmp(name,"_MathTransBase") == 0) mathtransused=TRUE;
  615.  if (strcmp(name,"_GfxBase") == 0) gfxused=TRUE;
  616.  if (strcmp(name,"_IntuitionBase") == 0) intuitionused=TRUE;
  617.  if (strcmp(name,"_TransBase") == 0) translateused=TRUE;
  618.  
  619.  /* allocate memory for a new node & name field */
  620.  if ((new_xref = (XREF *)alloc(sizeof(XREF),MEMF_ANY)) == NULL)
  621.  {
  622.   printf("Can't allocate memory for XREF node!\n");
  623.   early_exit=TRUE;
  624.   kill_all_lists();
  625.   cleanup();
  626.  }
  627.  
  628.  if ((new_xref->name=(char *)alloc(strlen(name)+1,MEMF_ANY))==NULL)
  629.  {
  630.   printf("Can't allocate memory for XREF node name field!\n");
  631.   early_exit=TRUE;
  632.   kill_all_lists();
  633.   cleanup();
  634.  }
  635.  
  636.  /* fill XREF struct */
  637.  strcpy(new_xref->name,name);
  638.  
  639.  new_xref->next = NULL;
  640.  curr_xref->next = new_xref;
  641.  curr_xref = curr_xref->next;
  642. }
  643.  
  644. void write_xrefs()
  645. {
  646. BOOL past_head=FALSE;
  647. XREF *curr,*temp;
  648.  
  649.  fprintf(dest,"\n");
  650.  
  651.  curr=xref;
  652.  do
  653.  {
  654.   temp=curr;
  655.   curr=curr->next;
  656.   if (past_head)
  657.   { 
  658.    /* xdef or xref? */
  659.    if (temp->name[0] != '*')
  660.    {
  661.     /* XREF */
  662.        fprintf(dest,"\txref %s\n",temp->name);
  663.    }
  664.    else
  665.    {
  666.     /* XDEF -> first replace '*' with '_' */
  667.     temp->name[0] = '_';
  668.     fprintf(dest,"\txdef %s\n",temp->name);
  669.    }
  670.   }
  671.   if (!past_head) past_head=TRUE; 
  672.  }
  673.  while (curr != NULL);
  674. }
  675.  
  676. /* --BASIC DATA list functions-- */
  677.  
  678. void enter_BASDATA(literal)
  679. char *literal;
  680. {
  681.  basdatapresent=TRUE;
  682.  
  683.  /* allocate memory for a new node & data */
  684.  if ((new_basdata = (BASDATA *)alloc(sizeof(BASDATA),MEMF_ANY)) == NULL)
  685.  {
  686.   printf("Can't allocate memory for BASIC DATA node!\n");
  687.   early_exit=TRUE;  
  688.   kill_all_lists();
  689.   cleanup();
  690.  }
  691.  
  692.  if ((new_basdata->literal=(char *)alloc(strlen(literal)+1,MEMF_ANY))==NULL)
  693.  {
  694.   printf("Can't allocate memory for BASIC DATA node literal field!\n");
  695.   early_exit=TRUE;
  696.   kill_all_lists();
  697.   cleanup();
  698.  }
  699.  
  700.  /* fill BASDATA struct */
  701.  strcpy(new_basdata->literal,literal);
  702.  
  703.  new_basdata->next = NULL;
  704.  curr_basdata->next = new_basdata;
  705.  curr_basdata = curr_basdata->next;
  706. }
  707.  
  708. void write_basdata()
  709. {
  710. BOOL    past_head=FALSE;
  711. BASDATA *curr,*temp;
  712.  
  713.  curr=basdata;
  714.  
  715.  if (curr->next != NULL)
  716.  {
  717.   fprintf(dest,"\n_BASICdata:\n");
  718.  }
  719.  
  720.  do
  721.  {
  722.   temp=curr;
  723.   curr=curr->next;
  724.   if (past_head) 
  725.   {
  726.    fprintf(dest,"\t%s\n",temp->literal);
  727.   }
  728.   if (!past_head) past_head=TRUE; 
  729.  }
  730.  while (curr != NULL);
  731. }
  732.  
  733.  
  734. /* --structure functions-- */
  735.  
  736. void find_structmem_tail(symtabitem)
  737. SYM *symtabitem;
  738. {
  739. /* find end of structdef member list. */
  740.  tail_structmem = symtabitem->structmem;
  741.  while (tail_structmem->next != NULL) tail_structmem = tail_structmem->next;
  742. }
  743.  
  744. BOOL structmem_exist(symtabitem,name)
  745. SYM  *symtabitem;
  746. char *name;
  747. {
  748. /* seek a structure 
  749.    member. 
  750. */
  751.  
  752.  curr_structmem = symtabitem->structmem->next; /* head has no member data */
  753.  
  754.  while (curr_structmem != NULL)
  755.  {
  756.   if (strcmp(curr_structmem->name,name) == 0) 
  757.      return(TRUE);
  758.   else
  759.      curr_structmem = curr_structmem->next;
  760.  }
  761.  
  762.  return(FALSE);   
  763. }
  764.  
  765. void add_struct_member(symtabitem,name,mtype,structtype)
  766. SYM  *symtabitem;
  767. char *name;
  768. int  mtype;
  769. SYM  *structtype;
  770. {
  771. /* add a member to a
  772.    structure definition.
  773. */
  774.  
  775.  if (structmem_exist(symtabitem,name))
  776.     _error(61);
  777.  else
  778.  {
  779.   /* add a unique member */
  780.   if ((new_structmem = (STRUCM *)alloc(sizeof(STRUCM),MEMF_ANY)) == NULL)
  781.   {
  782.    printf("Can't allocate memory for structdef node!\n");
  783.    early_exit=TRUE;
  784.    kill_all_lists();  
  785.    cleanup();
  786.   }
  787.   else
  788.   {
  789.    /* enter member data */
  790.    strcpy(new_structmem->name,name);
  791.    new_structmem->type = mtype;
  792.    new_structmem->offset = symtabitem->size;
  793.    
  794.    /* link member into list */
  795.    find_structmem_tail(symtabitem);
  796.  
  797.    new_structmem->next = NULL;
  798.    tail_structmem->next = new_structmem;
  799.    curr_structmem = new_structmem;
  800.       
  801.    /* increment size of structure 
  802.       and hence, find next offset. 
  803.    */
  804.    switch(mtype)
  805.    {
  806.     case bytetype   : symtabitem->size += 1; break;
  807.     case shorttype  : symtabitem->size += 2; break;
  808.     case longtype   : symtabitem->size += 4; break;
  809.     case singletype : symtabitem->size += 4; break;
  810.     case stringtype : symtabitem->size += MAXSTRLEN; 
  811.               curr_structmem->strsize = MAXSTRLEN;
  812.               break;
  813.     case structure  : symtabitem->size += structtype->size; 
  814.               curr_structmem->strsize = structtype->size;
  815.               break;
  816.    }
  817.   }     
  818.  }
  819. }
  820.